<?php
include('../../__global.php');
class aj_getComment extends ml_controller 
{
    private $_rid;
    private $_page;
    private $_pagesize;
    
    public function initParam(){
        $this->_rid=$this->input('rid','g');
        $this->_page=intval($this->input('page','g'));
        $this->_pagesize=intval($this->input('pagesize','g'));
        $this->_pagesize = $this->_pagesize > 0 ? $this->_pagesize : 4;
    }
    public function checkParam(){
        $this->check_referer();
        if(!ml_tool_resid::is_resid($this->_rid))
            $this->api_output(ML_RCODE_PARAM);
        if(substr($this->_rid,'9','1') != '1')
            $this->api_output(ML_RCODE_PARAM);
    }
    
    public function main(){
        $oComment=new ml_model_dbComment();
        
        $oRedis=new ml_model_rdsCount();
        $rs = $oRedis->getCommentCount($aRid);
        if(!$rs){
            $oComment->getCountsByRid($this->_rid);
            $data['totalNum']=$oComment->get_data();
            $data['totalNum']=$data['totalNum']['num'];
        }else{
            $data['totalNum']=$oRedis->get_data();
        }
        
        $oComment->getCountsByRid($this->_rid);
        $data['totalNum']=$oComment->get_data();
        $data['totalNum']=$data['totalNum']['num'];
        $data['html'] = '';
        
        if($data['totalNum'] == 0){
            $this->api_output(ML_RCODE_SUCC,$data);
        }
        
        $begin = ($this->_page-1) * $this->_pagesize;
        $rs=$oComment->getCommentsByRid($this->_rid,$begin,$this->_pagesize);
        $data['pageNum'] = ceil($data['totalNum']/$this->_pagesize);
        if(!$rs)
            $this->api_output(ML_RCODE_BUSY);
        $comments=$oComment->get_data();
        
        $oid=ml_tool_resid::resid2uid($this->_rid);
        
        $result=ml_tool_commentOutput::formatComment($comments);
        $vid=$this->__visitor['uid'];
        if($oid == $vid){
            $canDel=true;
        }
        foreach( $result  as  &$val ){
            if($canDel){
                $val['canDel']=true; 
            }else{
                if($vid == $val['uid']  ){
                    $val['canDel']=true; 
                }else{
                    $val['canDel']=false; 
                }
            }
            $html .= ml_tool_template::parseModTpl($val , 'showComment');
            unset($val);
        }
        $data['html'] = $html;
        $this->api_output(ML_RCODE_SUCC,$data);
    }
}
new aj_getComment();